import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
import { Message } from 'element-ui';
import { getToken, removeToken } from "@/utils/auth";
import router from '@/router';

/**
 * http请求工具类
 *
 * 请求拦截器 负责将客户端标识token值存储并放置在头部提交给服务端
 *
 * 响应拦截器 负责全局处理业务请求的网络或者业务错误
 */

// 创建axios的实例
const request = axios.create({
  baseURL: process.env.VUE_APP_URL,
  timeout: 10000 // 超时时间
});

request.interceptors.request.use(config => {
  config.headers['token'] = getToken();
  return config


}, error => {
  //出现异常
  return Promise.reject(error);
})

request.interceptors.response.use(
  (response) => {
    if(response.headers['content-type'] === 'application/octet-stream;charset=utf-8'){
      return response.data
    }
    if (response.data.code == 20001) {
        removeToken()
        router.push('/login')
        return false;
    }
    return response;
  }, error => {
    return Promise.reject(error);
})

export default request;

